const express = require("express");
const router = express.Router();
const { getAllUsers, adminDeleteUser } = require("../utils/sql");

router.get("/allusers", async (req, res) => {
  try {
    const users = await getAllUsers();
    res.status(200).json(users);
  } catch (error) {
    console.log(error);
    res.status(500).json({ message: "Internal Server Error" });
  }
});

router.post("/usersdelid", async (req, res) => {
  try {
    const { id } = req.body;
    const result = await adminDeleteUser(id);
    if (result.affectedRows === 0) {
      return res.status(404).json({ message: "User not found" });
    }
    res.status(200).json({ code: 200, message: "User deleted successfully" });
  } catch (error) {
    console.log(error);
    res.status(500).json({ message: "Internal Server Error" });
  }
});

module.exports = router;
